package adventure.placeable.trigger.results;

import adventure.placeable.Mobile;
import adventure.placeable.MobileImp;
import player.Player;
import adventure.room.RoomManager;
import adventure.room.RoomManagerEdit;
import colour.ColourUtil;
import server.util.Broadcaster;

/**
 *
 * @author Michael Hanns
 *
 */
public class ResultSpawnMobile extends TriggerResult implements Cloneable {

    private Mobile m;

    public ResultSpawnMobile(String result, Mobile m) {
        super(result);
        this.m = m;
    }

    public ResultSpawnMobile(String result, int uses, Mobile m) {
        super(result, uses);
        this.m = m;
    }
    
    public ResultSpawnMobile() {
        super();
        m = new MobileImp();
    }

    public void setMobile(Mobile mob) {
        m = mob;
    }

    public int getMobileID() {
        return m.getID();
    }

    @Override
    public String getDescription() {
        if (super.limitedUses()) {
            String desc = super.resultText + ", uses: " + super.usesRemaining()
                    + " / SPAWNMOBILE " + m.getName();
            return desc;
        } else {
            String desc = super.resultText + " / SPAWNMOBILE " + m.getName();
            return desc;
        }
    }

    @Override
    public String resultImplementation(RoomManager r, Player p, String i, boolean previousFailed, Broadcaster out) {
        if (allConditionsMet(r, p, i, previousFailed)) {
            ((RoomManagerEdit)r).addMobileToRoom(m.cloneThis(), p.x(), p.y(), p.z());
            useOnce();
            
            String output = replaceKeywords(p);
            
            out.broadcastLocalToPlayer(p, replaceYou(output, p, true));
            
            return replaceYou(output, p, false);    
        } else {
            return "";
        }
    }

    @Override
    public String getType() {
        return "SPAWNMOBILE";
    }

    @Override
    public String replaceKeywords(Player p) {
        return resultText
                .replace("[PLAYER]", ColourUtil.colourise(p.getName(), ColourUtil.player))
                .replace("[MOBILE]", ColourUtil.colourise(m.getName(), ColourUtil.mobile))
                .replace("[USES]", ColourUtil.colourise(super.usesRemaining()+"", ColourUtil.value));
    }

}
